home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Communication / NewsBase / Source / kanjiconv.c < prev    next >
C/C++ Source or Header  |  1993-01-12  |  6KB  |  261 lines

  1. /* kconv.c -  Japanese code converter
  2.    between two of EUC, JIS, and Shift-JIS code.
  3.    written by K.Handa  89.5.25 */
  4. /*
  5.  * modified by K.Miyai, ISR 1991.Dec.24 
  6.  */
  7. #include <streams/streams.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <sys/types.h>
  11.  
  12. int    i1, i2;
  13. int    c1, c2;
  14.  
  15. int   kanjiconv();
  16. int  j2e_conv();
  17. int  e2j_conv();
  18. int  e2j_conv_adj();
  19. int  e2e_adj();
  20.  
  21. #define ESC_CODE 033
  22. #define TOK1 '$'
  23. #define TOK2 'B'
  24. #define TOA1 '('
  25. #define TOA2 'J'
  26.  
  27. #define        MAXLEN_LINE    500
  28. /* 507 is supposed to be a maximum for nntp, but doesn't work */
  29.  
  30. /* #define TEST */
  31. #ifdef TEST
  32. int
  33. main(int argc, char **argv)
  34. {
  35.     char     conv_func[32];
  36.     int         conv_flag;
  37.     NXStream     *in, *out;
  38.     int        stdInFileDesc, stdOutFileDesc;
  39.  
  40.     /* input stream */
  41.     if (argc ==1 || argc > 3) {
  42.         fprintf(stderr, "Usage: kanjiconv -j/-e/-c/-a [filename]\n");
  43.     } else if (argc == 2) {
  44.     stdInFileDesc = fileno(stdin);
  45.     in = NXOpenFile(stdInFileDesc, NX_READONLY);
  46.     } else if ((in = NXMapFile((char *)*(argv+2), NX_READONLY)) == NULL) {
  47.     fprintf(stderr,"can not open file %s\n", *(argv+2));
  48.     return (-1);
  49.     }
  50.     /* output stream */
  51.     stdOutFileDesc = fileno(stdout);
  52.     out = NXOpenFile(stdOutFileDesc, NX_WRITEONLY);
  53.  
  54.     /* call kanji converter */
  55.     ++argv;
  56.     if ((char)**argv != '-') {
  57.       fprintf(stderr," invalid option\n");
  58.     }
  59.     switch ((char)*(*argv+1)) {
  60.     case 'e':
  61.       kanjiconv("jis_to_euc", in, out);
  62.     case 'j':
  63.       kanjiconv("euc_to_jis", in, out);
  64.     case 'c':
  65.       kanjiconv("euc_to_jis_adjustline", in, out);
  66.     default:
  67.       kanjiconv("euc_to_euc_adjustline", in, out);
  68.     }
  69.  
  70.     NXClose(in); NXClose(out);
  71.     return(0);
  72. }
  73. #endif TEST /*TEST*/
  74.  
  75. int
  76. kanjiconv (char *conv_option, NXStream *fin, NXStream *fout)
  77. {
  78.     if (strcmp(conv_option,"jis_to_euc") == 0)
  79.     j2e_conv(fin, fout);
  80.     else if (strcmp(conv_option,"euc_to_jis") == 0)
  81.     e2j_conv(fin, fout);
  82.     else if (strcmp(conv_option,"euc_to_jis_adjustline") == 0)
  83.     e2j_conv_adj(fin, fout);
  84.     else if (strcmp(conv_option,"euc_to_euc_adjustline") == 0)
  85.     e2e_adj(fin, fout);
  86.     else
  87.     return (-1);
  88.     
  89.     return 0;
  90. }
  91.  
  92. int
  93. j2e_conv (NXStream *fin, NXStream *fout)
  94. {
  95.     int kanji = 0;
  96.  
  97.     while ((char)(i1 = NXGetc(fin)) != EOF) {
  98.     if (kanji) {
  99.         if (i1 == ESC_CODE) {
  100.         if ((i1 = NXGetc(fin)) == TOA1) {
  101.             NXGetc(fin);
  102.             kanji = 0;
  103.         } else {
  104.             NXPutc(fout,ESC_CODE);
  105.             NXUngetc(fin);
  106.         }
  107.         } else if (0x21 <= i1 && i1 <= 0x7e) {
  108.         c1 = i1 | 0x80;
  109.         c2 = NXGetc(fin) | 0x80;
  110.         NXPutc(fout, c1); NXPutc(fout, c2);
  111.         } else
  112.         NXPutc(fout, i1);
  113.     } else {
  114.         if (i1 == ESC_CODE) {
  115.         if ((i1 = NXGetc(fin)) == TOK1) {
  116.             NXGetc(fin);
  117.             kanji = 1;
  118.         } else {
  119.             NXPutc(fout, ESC_CODE);
  120.             NXUngetc(fin);
  121.         }
  122.         } else
  123.         NXPutc(fout, i1);
  124.     }
  125.     }
  126.     return (0);
  127. }
  128.     
  129. int
  130. e2j_conv(NXStream *fin, NXStream *fout)
  131. {
  132.     int kanji = 0;
  133.  
  134.     while ((char)(i1 = NXGetc(fin)) != EOF) {
  135.     if (kanji) {
  136.         if (i1 & 0x80) {
  137.         c1 = i1 & 0x7f; c2 = NXGetc(fin) & 0x7f;
  138.         NXPutc(fout,c1); NXPutc(fout,c2);
  139.         } else {
  140.         NXPutc(fout,ESC_CODE); NXPutc(fout,TOA1); NXPutc(fout,TOA2);
  141.         NXPutc(fout,i1);
  142.         kanji = 0;
  143.         }
  144.     } else {
  145.         if ((i2=NXGetc(fin)) == EOF) {
  146.         NXPutc(fout, i1);
  147.         break;
  148.          } else if ((i1 & 0x80) && (i2 & 0x80)) {
  149.         NXPutc(fout,ESC_CODE); NXPutc(fout,TOK1); NXPutc(fout,TOK2);
  150.         c1 = i1 & 0x7f; c2 = i2 & 0x7f;
  151.         NXPutc(fout,c1); NXPutc(fout,c2);      
  152.         kanji = 1;
  153.         } else {
  154.         NXUngetc(fin);
  155.         NXPutc(fout,i1);
  156.         }
  157.     }
  158.     }
  159.     return (0);
  160. }
  161.  
  162.  
  163. int
  164. e2j_conv_adj(NXStream *fin, NXStream *fout)
  165. {
  166.     int         kanji = 0;
  167.     unsigned int    length = 0;
  168.     int        adj_flag = 0;
  169.     
  170.     while ((char)(i1 = NXGetc(fin)) != EOF) {
  171.     if (i1 == '\n') {
  172.         /* reset counting */
  173.         length = 0;
  174.     }
  175.     if (length > MAXLEN_LINE) {
  176.         /* now i1 is next to MAXLEN_LINE */
  177.         if (kanji) {
  178.         NXUngetc(fin);
  179.         /* end this line*/
  180.         NXPutc(fout,ESC_CODE); NXPutc(fout,TOA1); NXPutc(fout,TOA2);
  181.         NXPutc(fout,'\r'); NXPutc(fout,'\n');
  182.         /* start next line*/
  183.         NXPutc(fout,ESC_CODE); NXPutc(fout,TOK1); NXPutc(fout,TOK2);
  184.         length = 0;
  185.         } else {
  186.         NXUngetc(fin);
  187.         NXPutc(fout,'\r'); NXPutc(fout,'\n');
  188.         length = 0;
  189.         }
  190.         adj_flag = 1;
  191.         continue;
  192.     }
  193.         
  194.     if (kanji) {
  195.         if (i1 & 0x80) {
  196.         c1 = i1 & 0x7f; c2 = NXGetc(fin) & 0x7f;
  197.         NXPutc(fout,c1); NXPutc(fout,c2);
  198.         length += 2;
  199.         } else {
  200.         NXPutc(fout,ESC_CODE); NXPutc(fout,TOA1); NXPutc(fout,TOA2);
  201.         NXPutc(fout,i1);
  202.         kanji = 0;
  203.         length += 4;
  204.         }
  205.     } else {
  206.         if ((i2=NXGetc(fin)) == EOF) {
  207.         NXPutc(fout, i1);
  208.         length += 1;
  209.         break;
  210.          } else if ((i1 & 0x80) && (i2 & 0x80)) {
  211.         NXPutc(fout,ESC_CODE); NXPutc(fout,TOK1); NXPutc(fout,TOK2);
  212.         c1 = i1 & 0x7f; c2 = i2 & 0x7f;
  213.         NXPutc(fout,c1); NXPutc(fout,c2);      
  214.         kanji = 1;
  215.         length += 5;
  216.         } else {
  217.         NXUngetc(fin);
  218.         NXPutc(fout,i1);
  219.         length += 1;
  220.         }
  221.     }
  222.     }
  223.     return adj_flag;
  224. }
  225.  
  226. int
  227. e2e_adj(NXStream *fin, NXStream *fout)
  228. {
  229.     unsigned int    length = 0;
  230.     int        adj_flag = 0;
  231.  
  232.     while ((char)(i1 = NXGetc(fin)) != EOF) {
  233.     if (i1 == '\n') {
  234.         /* reset counting */
  235.         length = 0;
  236.     }
  237.     if (length > MAXLEN_LINE) {
  238.         /* now i1 is next to MAXLEN_LINE */
  239.         NXUngetc(fin);
  240.         /* end this line*/
  241.         NXPutc(fout,'\r'); NXPutc(fout,'\n');
  242.         length = 0;
  243.         adj_flag = 1;
  244.         continue;
  245.     }
  246.     if (i1 & 0x80) {
  247.         if ((i2=NXGetc(fin)) && 0x80) {
  248.         /* i1:i2 is kanji, so put out the pair */
  249.         NXPutc(fout,i1); NXPutc(fout,i2);
  250.         } else {
  251.         NXUngetc(i2);
  252.         NXPutc(fout,i1);
  253.         }
  254.     } else {
  255.         NXPutc(fout,i1);
  256.     }
  257.     }
  258.     return (adj_flag);
  259. }
  260.  
  261.